package com.primaryedu.ui.app.parentsch;

import com.primaryedu.R;
import com.primaryedu.bean.QuestiondetailsBean;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 问卷调查
 *
 * @author Thomas
 * @description
 * @data 2018年1月8日
 */
public class QuestionAdapter extends BaseAdapter implements View.OnClickListener {
    private final List<QuestiondetailsBean.DataBean.QuestionDetailListBean> list;
    private Callback mCallback;
    @SuppressWarnings("unused")
    private LayoutInflater inflater;
    @SuppressWarnings("unused")
    private Context context;
    private QuestionAdapter.ViewHolder viewHolder;
    private QuestiondetailsBean.DataBean.QuestionDetailListBean qd;
    private String opinionId;
    private QuestiondetailsBean.DataBean.QuestionDetailListBean bean;

    public QuestionAdapter(Activity context, List<QuestiondetailsBean.DataBean.QuestionDetailListBean> list, Callback callback) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        mCallback = callback;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewHolder = null;
        if (null == convertView) {
            viewHolder = new QuestionAdapter.ViewHolder();
            convertView = inflater.inflate(R.layout.item_question, null);
            viewHolder.question = (TextView) convertView.findViewById(R.id.tv_question);
            viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup);
            viewHolder.rb_opinion_one = (RadioButton) convertView.findViewById(R.id.rb_opinion_one);
            viewHolder.rb_opinion_two = (RadioButton) convertView.findViewById(R.id.rb_opinion_two);
            viewHolder.rb_opinion_three = (RadioButton) convertView.findViewById(R.id.rb_opinion_three);
            viewHolder.rb_opinion_four = (RadioButton) convertView.findViewById(R.id.rb_opinion_four);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (QuestionAdapter.ViewHolder) convertView.getTag();
        }
        qd = list.get(position);
        viewHolder.radioGroup.setOnCheckedChangeListener(new QuestionAdapter.OpinionRadioButtonListener());//给RadioGroup绑定监视器

        Log.i("yy", "getView: question=====" + qd.getQuestion());
        viewHolder.radioGroup.setId(position);
        viewHolder.question.setText(position + 1 + "." + qd.getQuestion());
        Log.i("yy", "getView: questionsize=====" + qd.getOptionList().size());
        if (qd.getOptionList().size() ==4) {
            viewHolder.rb_opinion_one.setText(qd.getOptionList().get(0).getOption());
            viewHolder.rb_opinion_two.setText(qd.getOptionList().get(1).getOption());
            viewHolder.rb_opinion_three.setText(qd.getOptionList().get(2).getOption());
            viewHolder.rb_opinion_four.setText(qd.getOptionList().get(3).getOption());
        }else if (qd.getOptionList().size() == 3){
            viewHolder.rb_opinion_one.setText(qd.getOptionList().get(0).getOption());
            viewHolder.rb_opinion_two.setText(qd.getOptionList().get(1).getOption());
            viewHolder.rb_opinion_three.setText(qd.getOptionList().get(2).getOption());
            viewHolder.rb_opinion_four.setVisibility(View.GONE);
        }else if (qd.getOptionList().size() == 2){
            viewHolder.rb_opinion_one.setText(qd.getOptionList().get(0).getOption());
            viewHolder.rb_opinion_two.setText(qd.getOptionList().get(1).getOption());
            viewHolder.rb_opinion_three.setVisibility(View.GONE);
            viewHolder.rb_opinion_four.setVisibility(View.GONE);
        }

        if (qd.getSelectOptionId() == qd.getOptionList().get(0).getId()) {
            viewHolder.rb_opinion_one.setChecked(true);
            qd.setQue_state(1);
        } else if (qd.getOptionList().size() >= 2) {
            if (qd.getSelectOptionId() == qd.getOptionList().get(1).getId()) {
                viewHolder.rb_opinion_two.setChecked(true);
                qd.setQue_state(1);
            }
        } else if (qd.getOptionList().size() >= 3){
            if (qd.getSelectOptionId() == qd.getOptionList().get(2).getId()) {
                viewHolder.rb_opinion_three.setChecked(true);
                qd.setQue_state(1);
            }
        } else if (qd.getOptionList().size() == 4){
            if (qd.getSelectOptionId() == qd.getOptionList().get(3).getId()) {
                viewHolder.rb_opinion_four.setChecked(true);
                qd.setQue_state(1);
            }
        }

        viewHolder.radioGroup.setOnClickListener(this);
        viewHolder.rb_opinion_one.setOnClickListener(this);
        viewHolder.rb_opinion_two.setOnClickListener(this);
        viewHolder.rb_opinion_three.setOnClickListener(this);
        viewHolder.rb_opinion_four.setOnClickListener(this);
        viewHolder.radioGroup.setTag(position);
        viewHolder.rb_opinion_one.setTag(position);
        viewHolder.rb_opinion_two.setTag(position);
        viewHolder.rb_opinion_three.setTag(position);
        viewHolder.rb_opinion_four.setTag(position);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        mCallback.click(v);
    }

    public interface Callback {
        public void click(View v);
    }

    class ViewHolder {
        TextView question;
        RadioGroup radioGroup;
        RadioButton rb_opinion_one;
        RadioButton rb_opinion_two;
        RadioButton rb_opinion_three;
        RadioButton rb_opinion_four;
    }

    class OpinionRadioButtonListener implements RadioGroup.OnCheckedChangeListener {


        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            bean = list.get(group.getId());
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.rb_opinion_one:
                    // 当用户选择A
//					opinionId = qd.getOptionList().get(0).getId();
                    bean.setOpinions(0);
                    bean.setQue_state(1);
//					showToast("opinionStatus=1");
                    break;
                case R.id.rb_opinion_two:
                    // 当用户选择B
//					opinionId = qd.getOptionList().get(1).getId();
                    bean.setOpinions(1);
                    bean.setQue_state(1);
//					showToast("opinionStatus=2");
                    break;
                case R.id.rb_opinion_three:// 当用户选择C
//					opinionId = qd.getOptionList().get(2).getId();
                    bean.setOpinions(2);
                    bean.setQue_state(1);
                    break;
                case R.id.rb_opinion_four:// 当用户选择D
//					opinionId = qd.getOptionList().get(3).getId();
                    bean.setOpinions(3);
                    bean.setQue_state(1);
                    break;
            }
        }
    }


}
